// src/views/login/component/bind-phone.vue
<template>
	<div class="bind-phone-container">
		<div class="back-button" @click="$emit('back')">
			<el-icon>
				<ArrowLeft />
			</el-icon>
			<span>返回登录</span>
		</div>

		<!-- 绑定手机号标题和说明 -->
		<div class="bind-phone-header">
			<h2 class="bind-title">绑定手机号</h2>
			<p class="bind-subtitle">为了您的账户安全，请绑定您的手机号码</p>
		</div>

		<el-form ref="formRef" :model="form" :rules="rules" size="large" class="login-content-form theme-chemical">
			<el-form-item prop="Mobile" class="login-animation1">
				<el-input v-model="form.Mobile" placeholder="请输入手机号" clearable autocomplete="off" class="login-input">
					<template #prefix>
						<el-icon class="input-icon">
							<Phone />
						</el-icon>
					</template>
				</el-input>
			</el-form-item>

			<el-form-item prop="SmsCode" class="login-animation2">
				<div class="verification-code">
					<el-input v-model="form.SmsCode" placeholder="请输入验证码" clearable autocomplete="off" class="login-input" @keyup.enter="handleSubmit">
						<template #prefix>
							<el-icon class="input-icon">
								<Key />
							</el-icon>
						</template>
					</el-input>
					<el-button :disabled="!!timer" @click="handleSendCode" class="code-button">
						{{ timer ? `${countdown}s` : '获取验证码' }}
					</el-button>
				</div>
			</el-form-item>

			<el-form-item class="login-animation3">
				<el-button type="primary" class="login-content-submit" :loading="loading" @click="handleSubmit" size="large">
					<span v-if="!loading">确认绑定</span>
					<span v-else>正在绑定...</span>
				</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script setup lang="ts">
import { ref, onUnmounted } from 'vue';
import { ElMessage } from 'element-plus';
import { Phone, Key, ArrowLeft,  Message, Lock } from '@element-plus/icons-vue';
import type { FormInstance } from 'element-plus';
import useLoginApi from '/@/api/login';

import { Local } from '/@/utils/storage';

const emit = defineEmits(['loading-change', 'bind-success', 'back']);
const props = defineProps<{
	WechatKey: string;
}>();

const loginApi = useLoginApi();
const formRef = ref<FormInstance>();
const loading = ref(false);
const timer = ref<number | null>(null);
const countdown = ref(60);

const form = ref({
	Mobile: '',
	SmsCode: '',
});

const rules = {
	Mobile: [
		{ required: true, message: '请输入手机号', trigger: 'blur' },
		{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
	],
	SmsCode: [
		{ required: true, message: '请输入验证码', trigger: 'blur' },
		{ pattern: /^\d{6}$/, message: '验证码格式错误', trigger: 'blur' },
	],
};

// 发送验证码
const handleSendCode = async () => {
	if (!form.value.Mobile) {
		ElMessage.warning('请先输入手机号');
		return;
	}
	if (!/^1[3-9]\d{9}$/.test(form.value.Mobile)) {
		ElMessage.warning('请输入正确的手机号');
		return;
	}

	try {
		const res = await loginApi.sendVerificationCode(form.value.Mobile, 'login');
		if (res.Code === 200) {
			ElMessage.success('验证码发送成功');
			// 开始倒计时
			countdown.value = 60;
			timer.value = window.setInterval(() => {
				if (countdown.value > 0) {
					countdown.value--;
				} else {
					if (timer.value) {
						clearInterval(timer.value);
						timer.value = null;
					}
				}
			}, 1000);
		} else {
			ElMessage.error(res.Message || '发送失败');
		}
	} catch (error) {
		ElMessage.error('发送验证码失败');
	}
};

// 提交绑定
const handleSubmit = async () => {
	if (!formRef.value) return;

	await formRef.value.validate(async (valid) => {
		if (valid) {
			loading.value = true;
			emit('loading-change', true);

			try {
				const res = await loginApi.bindPhone({
					Mobile: form.value.Mobile,
					SmsCode: form.value.SmsCode,
					WechatKey: props.WechatKey,
				});
				if (res.Code === 200) {
					// await handleLoginSuccess(res.Data.access_token, emit);
					Local.set('token', res.Data.access_token);
					window.location.replace('/');
					ElMessage.success('登录成功');
					emit('bind-success', res.Data);
				} else {
					ElMessage.error(res.Message || '绑定失败');
				}
			} catch (error) {
				ElMessage.error('绑定失败，请重试');
			} finally {
				loading.value = false;
				emit('loading-change', false);
			}
		}
	});
};

onUnmounted(() => {
	if (timer.value) {
		clearInterval(timer.value);
		timer.value = null;
	}
});
</script>

<style scoped>
/* 基于 account.vue 的输入框修复样式 */

.bind-phone-container {
  position: relative;
  width: 100%;
}

.back-button {
  position: absolute;
  left: 0;
  top: -50px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: #6b7280;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  padding: 8px 12px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.back-button:hover {
  color: #1e40af;
  background: rgba(30, 64, 175, 0.1);
  transform: translateX(-2px);
}

.back-button .el-icon {
  font-size: 16px;
}

/* 绑定手机号标题和说明样式 */
.bind-phone-header {
  text-align: center;
  margin: 2rem 0 3rem 0;
  padding: 0 1rem;
}

.bind-title {
  font-size: 2rem;
  font-weight: 700;
  color: #1f2937;
  margin: 0 0 0.75rem 0;
  letter-spacing: -0.025em;
  background: linear-gradient(135deg, #1e40af 0%, #059669 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.bind-subtitle {
  font-size: 1rem;
  color: #6b7280;
  margin: 0 0 2rem 0;
  line-height: 1.6;
  font-weight: 400;
}

.security-tips {
  display: flex;
  justify-content: center;
  gap: 2rem;
  flex-wrap: wrap;
  margin-top: 1.5rem;
}

.tip-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem;
  background: rgba(30, 64, 175, 0.05);
  border-radius: 12px;
  border: 1px solid rgba(30, 64, 175, 0.1);
  transition: all 0.3s ease;
  min-width: 120px;
}

.tip-item:hover {
  background: rgba(30, 64, 175, 0.1);
  border-color: rgba(30, 64, 175, 0.2);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(30, 64, 175, 0.15);
}

.tip-icon {
  font-size: 24px;
  color: #1e40af;
  padding: 8px;
  background: rgba(30, 64, 175, 0.1);
  border-radius: 50%;
  transition: all 0.3s ease;
}

.tip-item:hover .tip-icon {
  background: rgba(30, 64, 175, 0.2);
  transform: scale(1.1);
}

.tip-item span {
  font-size: 0.875rem;
  color: #374151;
  font-weight: 500;
  text-align: center;
}

/* 登录表单容器 */
.login-content-form {
  width: 100%;
}

/* 表单项样式 */
.login-content-form .el-form-item {
  margin-bottom: 1.5rem;
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

.login-content-form .el-form-item__content {
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

/* 验证码输入框容器 */
.verification-code {
  display: flex;
  gap: 12px;
  align-items: center;
}

.verification-code .login-input {
  flex: 1;
}

.code-button {
  height: 56px;
  padding: 0 20px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 12px;
  background: #1e40af;
  border: 2px solid #1e40af;
  color: white;
  transition: all 0.3s ease;
  white-space: nowrap;
  min-width: 120px;
}

.code-button:hover {
  background: #1e3a8a;
  border-color: #1e3a8a;
  transform: translateY(-1px);
}

.code-button:disabled {
  background: #9ca3af;
  border-color: #9ca3af;
  cursor: not-allowed;
  transform: none;
}

/* 输入框样式 - 基于 account.vue 的成功方案 */
.login-input {
  height: 56px;
  position: relative;
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
  width: 100% !important;
}

.login-input :deep(.el-input) {
  height: 56px;
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
  width: 100% !important;
}

.login-input :deep(.el-input__wrapper) {
  width: 100% !important;
  height: 56px !important;
  background: #ffffff !important;
  border: 2px solid #e5e7eb !important;
  border-radius: 12px !important;
  padding: 0 20px 0 55px !important;
  transition: all 0.3s ease !important;
  box-shadow: none !important;
  display: flex !important;
  visibility: visible !important;
  opacity: 1 !important;
  position: relative !important;
}

.login-input :deep(.el-input__wrapper):hover {
  border-color: #1e40af !important;
  transform: translateY(-1px);
}

.login-input :deep(.el-input__wrapper.is-focus) {
  outline: none !important;
  border-color: #1e40af !important;
  box-shadow: 0 0 0 4px rgba(30, 64, 175, 0.1) !important;
}

/* 输入框内部文字样式 - 强制修复 */
.login-input :deep(.el-input__inner) {
  width: 100% !important;
  height: 52px !important;
  background: transparent !important;
  border: none !important;
  color: #1f2937 !important;
  font-size: 14px !important;
  font-weight: 500 !important;
  transition: all 0.3s ease !important;
  outline: none !important;
  box-shadow: none !important;
  padding: 0 !important;
  line-height: 52px !important;
}

.login-input :deep(.el-input__inner)::placeholder {
  color: #9ca3af !important;
  font-weight: 400 !important;
}

.login-input :deep(.el-input__inner):focus {
  color: #1f2937 !important;
}

/* 强制修复所有可能的文字颜色问题 */
.login-input :deep(.el-input__inner),
.login-input :deep(.el-input__inner):focus,
.login-input :deep(.el-input__inner):active,
.login-input :deep(.el-input__inner):hover,
.login-input :deep(.el-input__inner):valid,
.login-input :deep(.el-input__inner):invalid,
.login-input :deep(.el-input__inner):not(:placeholder-shown) {
  color: #1f2937 !important;
  background: transparent !important;
}

/* 覆盖任何白色文字样式 */
.login-input :deep(.el-input__inner[style*="color: white"]),
.login-input :deep(.el-input__inner[style*="color: #fff"]),
.login-input :deep(.el-input__inner[style*="color: #ffffff"]) {
  color: #1f2937 !important;
}

/* 自动填充样式修复 */
.login-input :deep(.el-input__inner:-webkit-autofill),
.login-input :deep(.el-input__inner:-webkit-autofill:hover),
.login-input :deep(.el-input__inner:-webkit-autofill:focus),
.login-input :deep(.el-input__inner:-webkit-autofill:active) {
  -webkit-box-shadow: 0 0 0 1000px #ffffff inset !important;
  -webkit-text-fill-color: #1f2937 !important;
  transition: background-color 5000s ease-in-out 0s !important;
  color: #1f2937 !important;
  font-weight: 500 !important;
  font-size: 14px !important;
}

/* 前缀图标样式 - 修复图标显示问题 */
.login-input :deep(.el-input__prefix) {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  padding-right: 12px !important;
  left: 12px !important;
  position: absolute !important;
  top: 50% !important;
  transform: translateY(-50%) !important;
  z-index: 10 !important;
  width: 40px !important;
  height: 40px !important;
}

.login-input :deep(.el-input__prefix .el-icon) {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  color: #6b7280 !important;
  font-size: 18px !important;
  transition: all 0.3s ease !important;
  width: 20px !important;
  height: 20px !important;
}

.login-input :deep(.el-input__prefix .input-icon) {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  color: #6b7280 !important;
  font-size: 18px !important;
  transition: all 0.3s ease !important;
  width: 20px !important;
  height: 20px !important;
}

.login-input:focus-within :deep(.el-input__prefix .el-icon),
.login-input:focus-within :deep(.el-input__prefix .input-icon) {
  color: #1e40af !important;
  transform: scale(1.05);
}

/* 后缀图标样式 - 修复清除和密码显示按钮 */
.login-input :deep(.el-input__suffix) {
  position: absolute !important;
  right: 12px !important;
  top: 50% !important;
  transform: translateY(-50%) !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  z-index: 10 !important;
  height: 40px !important;
}

.login-input :deep(.el-input__suffix-inner) {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 8px !important;
}

.login-input :deep(.el-input__suffix-inner .el-input__clear),
.login-input :deep(.el-input__suffix-inner .el-input__password) {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  color: #6b7280 !important;
  font-size: 16px !important;
  transition: all 0.3s ease !important;
  cursor: pointer !important;
  width: 20px !important;
  height: 20px !important;
  border-radius: 4px !important;
  background: transparent !important;
  border: none !important;
}

.login-input :deep(.el-input__suffix-inner .el-input__clear):hover,
.login-input :deep(.el-input__suffix-inner .el-input__password):hover {
  color: #1e40af !important;
  background: rgba(30, 64, 175, 0.1) !important;
  transform: scale(1.1);
}

/* 修复Element Plus图标组件 */
.login-input :deep(.el-input__suffix-inner .el-icon) {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  color: #6b7280 !important;
  font-size: 16px !important;
  transition: all 0.3s ease !important;
  cursor: pointer !important;
  width: 20px !important;
  height: 20px !important;
  border-radius: 4px !important;
}

.login-input :deep(.el-input__suffix-inner .el-icon):hover {
  color: #1e40af !important;
  background: rgba(30, 64, 175, 0.1) !important;
  transform: scale(1.1);
}

/* 登录按钮样式 */
.login-content-submit {
  width: 100%;
  height: 56px;
  font-size: 0.9rem;
  font-weight: 700;
  border-radius: 12px;
  background: linear-gradient(135deg, #1e40af 0%, #059669 100%);
  border: 2px solid #1e40af;
  color: white !important;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  margin-top: 1rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  cursor: pointer;
}

.login-content-submit:hover {
  background: linear-gradient(135deg, #1e3a8a 0%, #059669 100%);
  transform: translateY(-2px);
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  border-color: #1e3a8a;
}

.login-content-submit:active {
  transform: translateY(-1px);
}

.login-content-submit.is-loading {
  background: #9ca3af;
  border-color: #9ca3af;
  cursor: not-allowed;
  transform: none;
}

.login-content-submit.is-loading:hover {
  background: #9ca3af;
  transform: none;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  border-color: #9ca3af;
}

/* 动画效果 */
.login-animation1 {
  animation: slideInUp 0.6s ease-out;
}

.login-animation2 {
  animation: slideInUp 0.6s ease-out 0.1s both;
}

.login-animation3 {
  animation: slideInUp 0.6s ease-out 0.2s both;
}

@keyframes slideInUp {
  0% {
    opacity: 0;
    transform: translateY(20px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 暗黑模式支持 */
[data-theme='dark'] .back-button {
  color: #9ca3af;
  background: rgba(30, 41, 59, 0.5);
}

[data-theme='dark'] .back-button:hover {
  color: #3b82f6;
  background: rgba(59, 130, 246, 0.1);
}

[data-theme='dark'] .login-input :deep(.el-input__wrapper) {
  background: #1e293b !important;
  border-color: #475569 !important;
}

[data-theme='dark'] .login-input :deep(.el-input__inner) {
  color: #f9fafb !important;
}

[data-theme='dark'] .login-input :deep(.el-input__inner)::placeholder {
  color: #6b7280 !important;
}

[data-theme='dark'] .login-input :deep(.el-input__prefix .el-icon),
[data-theme='dark'] .login-input :deep(.el-input__prefix .input-icon) {
  color: #9ca3af !important;
}

[data-theme='dark'] .login-input:focus-within :deep(.el-input__prefix .el-icon),
[data-theme='dark'] .login-input:focus-within :deep(.el-input__prefix .input-icon) {
  color: #3b82f6 !important;
}

[data-theme='dark'] .login-input :deep(.el-input__suffix-inner .el-input__clear),
[data-theme='dark'] .login-input :deep(.el-input__suffix-inner .el-input__password),
[data-theme='dark'] .login-input :deep(.el-input__suffix-inner .el-icon) {
  color: #9ca3af !important;
}

[data-theme='dark'] .login-input :deep(.el-input__suffix-inner .el-input__clear):hover,
[data-theme='dark'] .login-input :deep(.el-input__suffix-inner .el-input__password):hover,
[data-theme='dark'] .login-input :deep(.el-input__suffix-inner .el-icon):hover {
  color: #3b82f6 !important;
  background: rgba(59, 130, 246, 0.1) !important;
}

[data-theme='dark'] .login-input :deep(.el-input__inner:-webkit-autofill),
[data-theme='dark'] .login-input :deep(.el-input__inner:-webkit-autofill:hover),
[data-theme='dark'] .login-input :deep(.el-input__inner:-webkit-autofill:focus),
[data-theme='dark'] .login-input :deep(.el-input__inner:-webkit-autofill:active) {
  -webkit-box-shadow: 0 0 0 1000px #1e293b inset !important;
  -webkit-text-fill-color: #f9fafb !important;
}

[data-theme='dark'] .code-button {
  background: #3b82f6;
  border-color: #3b82f6;
}

[data-theme='dark'] .code-button:hover {
  background: #2563eb;
  border-color: #2563eb;
}

/* 暗黑模式下的标题样式 */
[data-theme='dark'] .bind-title {
  color: #f9fafb;
  background: linear-gradient(135deg, #3b82f6 0%, #10b981 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

[data-theme='dark'] .bind-subtitle {
  color: #9ca3af;
}

[data-theme='dark'] .tip-item {
  background: rgba(59, 130, 246, 0.1);
  border-color: rgba(59, 130, 246, 0.2);
}

[data-theme='dark'] .tip-item:hover {
  background: rgba(59, 130, 246, 0.15);
  border-color: rgba(59, 130, 246, 0.3);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
}

[data-theme='dark'] .tip-icon {
  color: #3b82f6;
  background: rgba(59, 130, 246, 0.15);
}

[data-theme='dark'] .tip-item:hover .tip-icon {
  background: rgba(59, 130, 246, 0.25);
}

[data-theme='dark'] .tip-item span {
  color: #e5e7eb;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bind-phone-header {
    margin: 1.5rem 0 2rem 0;
    padding: 0 0.5rem;
  }

  .bind-title {
    font-size: 1.75rem;
  }

  .bind-subtitle {
    font-size: 0.9rem;
  }

  .security-tips {
    gap: 1rem;
    margin-top: 1rem;
  }

  .tip-item {
    min-width: 100px;
    padding: 0.75rem;
  }

  .tip-icon {
    font-size: 20px;
    padding: 6px;
  }

  .tip-item span {
    font-size: 0.8rem;
  }

  .back-button {
    top: -40px;
    font-size: 13px;
    padding: 6px 10px;
  }
}

@media (max-width: 480px) {
  .security-tips {
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
  }

  .tip-item {
    flex-direction: row;
    justify-content: flex-start;
    width: 100%;
    max-width: 280px;
    text-align: left;
  }

  .tip-icon {
    margin-right: 0.5rem;
  }
}
</style>
